import { logger } from 'ee-core/log';
import { sqlitedbService } from '../service/database/sqlitedb';
import { userdbService } from '../service/database/userdb';
import { Result } from '../common/result';

/**
 * user - demo
 * @class
 */
class UserController {

    /**
     * 添加用户
     */
    async add(args: { info: { name: string; age: number } }) {
        try {
            const result = await userdbService.addUser(args.info);
            return Result.ok({ result });
        } catch (error) {
            return Result.error(error instanceof Error ? error.message : '添加用户失败');
        }
    }

    /**
     * 删除用户
     */
    async delete(args: { name: string }) {
        try {
            const result = await userdbService.deleteUser(args.name);
            return Result.ok({ result });
        } catch (error) {
            return Result.error(error instanceof Error ? error.message : '删除用户失败');
        }
    }

    /**
     * 更新用户
     */
    async update(args: { name: string; age: number }) {
        try {
            const result = await userdbService.updateUser(args.name, args.age);
            return Result.ok({ result });
        } catch (error) {
            return Result.error(error instanceof Error ? error.message : '更新用户失败');
        }
    }

    /**
     * 查询用户
     */
    async get(args: { search_age: number })
    {
        try {
            const users = await userdbService.getUser(args.search_age);
            return Result.ok( users );
        } catch (error) {
            return Result.error(error instanceof Error ? error.message : '查询用户失败');
        }
    }

    /**
     * 查询所有用户
     */
    async getAll() {
        try {
            const allUsers = await userdbService.getAll();
            return Result.ok( allUsers );
        } catch (error) {
            return Result.error(error instanceof Error ? error.message : '查询所有用户失败');
        }
    }

    /**
     * sqlite数据库操作
     */
    async sqlitedbOperation(args: {
        action: string;
        info?: any;
        delete_name?: string;
        update_name?: string;
        update_age?: number;
        search_age?: number;
        data_dir?: string;
    }): Promise<{
        action: string;
        result: any;
        all_list: any;
        code: number;
    }> {
        const { action, info, delete_name, update_name, update_age, search_age, data_dir } = args;

        const data = {
            action,
            result: null as any,
            all_list: []as any,
            code: 0
        };

        try {
            // test
            sqlitedbService.getDataDir();
        } catch (err) {
            console.log(err);
            data.code = -1;
            return data;
        }

        switch (action) {
            case 'add' :
                data.result = await sqlitedbService.addTestDataSqlite(info);;
                break;
            case 'del' :
                data.result = await sqlitedbService.delTestDataSqlite(delete_name);;
                break;
            case 'update' :
                data.result = await sqlitedbService.updateTestDataSqlite(update_name, update_age);
                break;
            case 'get' :
                data.result = await sqlitedbService.getTestDataSqlite(search_age);
                break;
            case 'getDataDir' :
                data.result = await sqlitedbService.getDataDir();
                break;
            case 'setDataDir' :
                data.result = await sqlitedbService.setCustomDataDir(data_dir || "");
                break;
        }

        data.all_list = await sqlitedbService.getAllTestDataSqlite();

        return data;
    }

    /**
     * 测试接口
     */
    hello(args: any): void {
        logger.info('hello ', args);
    }
}
UserController.toString = () => '[class UserController]';

export default UserController;  